package com.laxcen.lib;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.laxcen.intelligenttoy.BLE.OpenBleProgreessDialog;
import com.laxcen.toydemo.GameStep4Activity;
import com.laxcen.toydemo.R;

import util.DelayHandler;
import util.SoundClass;

public class GameSuccessDialog extends CustomViewDialog implements View.OnClickListener {

    private static GameSuccessDialog dialog;

    public static GameSuccessDialog getInstance(Context context) {
        //双重锁模式，确保单例创建
        if (dialog == null) {
            synchronized (OpenBleProgreessDialog.class) {
                if (dialog == null) {
                    dialog = new GameSuccessDialog(context);
                    dialog.setCancelable(false);
                    dialog.create();
                }
            }
        }
        return dialog;
    }

    private TextView txtvName, txtvSummary;
    private Button btnClose, btn_goback, btn_next;
    private ImageView imgScore1, imgScore2, imgScore3, imgScore4, imgScore5;

    public GameSuccessDialog(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutViewId() {
        return R.layout.dialog_game_success;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }


    @Override
    protected void initViewControls() {
        super.initViewControls();
        txtvName = findViewById(R.id.txtvName);
        btnClose = findViewById(R.id.btnClose);
        if (btnClose != null) btnClose.setOnClickListener(this);

        btn_goback = findViewById(R.id.btn_goback);
        if (btn_goback != null) btn_goback.setOnClickListener(this);
        btn_next = findViewById(R.id.btn_next);
        if (btn_next != null) btn_next.setOnClickListener(this);

        imgScore1 = findViewById(R.id.imgScore1);
        imgScore2 = findViewById(R.id.imgScore2);
        imgScore3 = findViewById(R.id.imgScore3);
        imgScore4 = findViewById(R.id.imgScore4);
        imgScore5 = findViewById(R.id.imgScore5);


        //###2###，############
        setCancelableOnBackKey(false);
        setCancelable(true);

        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                new SoundClass(customViewContext).playSound(R.raw.game_step_success);
            }
        }, 100).delay(new Runnable() {
            @Override
            public void run() {
                onClick(btn_next);
            }
        }, 7000);


    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnClose) {
            dismiss();
        } else if (v.getId() == R.id.btn_goback) {
            if (gobackClick != null) gobackClick.onClick(v);
            dismiss();
        } else if (v.getId() == R.id.btn_next) {
            if (goNextClick != null) goNextClick.onClick(v);
            dismiss();
        }
    }


    @Override
    protected void initViewDatas() {

    }

    @Override
    public synchronized void dismiss() {
        super.dismiss();
        dialog = null;
    }

    @Override
    public void show() {
        super.show();
    }


    public void show(String msg, int score) {
        this.show();
        setScore(score);

    }

    private void setScore(int score) {
        if (score > 0)
            imgScore1.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_color));
        else
            imgScore1.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_gray));

        if (score > 1)
            imgScore2.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_color));
        else
            imgScore2.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_gray));

        if (score > 2)
            imgScore3.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_color));
        else
            imgScore3.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_gray));

        if (score > 3)
            imgScore4.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_color));
        else
            imgScore4.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_gray));

        if (score > 4)
            imgScore5.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_color));
        else
            imgScore5.setImageDrawable(ContextCompat.getDrawable(customViewContext, R.mipmap.star_gray));
    }

    View.OnClickListener gobackClick;

    public void setGobackClick(View.OnClickListener listener) {
        gobackClick = listener;
    }

    View.OnClickListener goNextClick;

    public void setGoNextClick(View.OnClickListener listener) {
        goNextClick = listener;
    }
}

